package socketmvc.core.util;

import socketmvc.core.adapter.SocketComponentAdapter;
import socketmvc.core.packets.basic.IPacket;

import java.nio.ByteBuffer;

public class PacketHandler {

    private final SocketComponentAdapter componentAdapter;

    public PacketHandler(SocketComponentAdapter componentAdapter) {
        this.componentAdapter = componentAdapter;
    }

    public ByteBuffer toByteBuffer(IPacket packet){
        return componentAdapter.identifyCodec(packet).encode(packet);
    }

    public IPacket toPacket(ByteBuffer buffer){
        return componentAdapter.identifyCodec(buffer).decode(buffer);
    }
}
